nedcomp hosting homepage

Producten en Diensten
Dedicated servers
Datacenter informatie
Partners, resellers
Helpdesk informatie
Technische docs, tools
Support homepage
ASP componenten
Praktische ASP, ASP.NET
Visual route server
Whois (domein gegevens)
Software documentatie
Whitepapers
Zoeken
Nedcomp / algemeen

Zoeken
 

Copyright © Nedcomp Hosting
Telefoon nr :   +31 184 670111
Fax nummer :   +31 184 631384
E-mailadres :   info@nedcomp.nl
 

C# Programmer's Reference  

[] Operator

Square brackets ([]) are used for arrays, indexers, and attributes. They can also be used with pointers.

type []
array [ indexexpr ]

Where:

type
A type.
array
An array.
indexexpr
An index expression.

Remarks

An array type is a type followed by []:

int[] fib; // fib is of type int[], "array of int"
fib = new int[100]; // create a 100-element int array

To access an element of an array, the index of the desired element is enclosed in brackets:

fib[0] = fib[1] = 1;
for( int i=2; i<100; ++i ) fib[i] = fib[i-1] + fib[i-2];

An exception is thrown if an array index is out of range.

The array indexing operator cannot be overloaded; however, types can define indexers, properties that take one or more parameters. Indexer parameters are enclosed in square brackets, just like array indices, but indexer parameters can be declared to be of any type (unlike array indices, which must be integral).

For example, the .NET Framework defines a Hashtable type that associates keys and values of arbitrary type:

Collections.Hashtable h =  new Collections.Hashtable();
h["a"] = 123; // note: using a string as the index

Square brackets are also used to specify attributes:

[attribute(AllowMultiple=true)]
public class Attr {
}

You can use square brackets to index off a pointer (see A.2 Pointer types):

unsafe fixed ( int* p = fib )   // p points to fib from earlier example
{
   p[0] = p[1] = 1;
   for( int i=2; i<100; ++i ) p[i] = p[i-1] + p[i-2];
}

No bounds checking is performed.

See Also

C# Operators | Arrays | Indexers | unsafe | fixed